package C058;

/**
 * ClassName: c01
 * Package: C058
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/3 09:10
 * @Version 1.0
 * @Since 1.0
 */
public class c01 {

    public static void main(String[] args) {
        char[][] arr = {
                {'1', '1', '0', '0', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '1', '0', '0'},
                {'0', '0', '0', '1', '1'}
        };
        System.out.println(numIslands(arr));
    }

    static int res = 0;
    static String resStr = "123";
    static String resStr2 = "123";
    public static int numIslands(char[][] grid) {
        res = 0;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == '1') {
                    dfs(grid, i, j);
                    res ++;
                }
            }
        }

        return res;
    }

    private static void dfs(char[][] grid, int i, int j) {
        if(i == -1 || i == grid.length || j == - 1 || j == grid[i].length || grid[i][j] != '1') {
            return;
        }
        grid[i][j] = '2';
        dfs(grid, i - 1, j);
        dfs(grid, i + 1, j);
        dfs(grid, i, j - 1);
        dfs(grid, i, j + 1);
    }
}
